package com.emm.yixun.estate.service.impl;
import java.util.List;
import javax.jws.WebService;

import com.emm.yixun.common.utils.BeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.ProjectParameterService;
import com.emm.yixun.common.exception.ResponseCode;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.model.ProjectParameter;
import com.emm.yixun.common.model.dto.ProjectParameterDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.estate.service.ProjectParameterServiceFacade;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;

@Service("projectParameterServiceFacade")
@WebService(name="com.emm.yixun.estate.service.ProjectParameterServiceFacade")
public class ProjectParameterServiceFacadeImpl implements ProjectParameterServiceFacade{
	private  final Logger   logger = LoggerFactory.getLogger(this.getClass()); 
	
	@Autowired
	private ProjectParameterService service;

	@Override
	public Response<ProjectParameterDto> save(ProjectParameterDto dto, String json) {
		Response<ProjectParameterDto> response=new Response<ProjectParameterDto>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("保存"));
				return response;
			}
			//先删除原项目参数
			ProjectParameterDto del=new ProjectParameterDto();
			del.setMerchantId(dto.getMerchantId());
			del.setProjectId(dto.getProjectId());
			del.setDelFlag(DelFlagEnum.UNDEL.getNo());
			int count=service.delByProjectId(del);
			//循环保存新项目参数
			JSONArray array = JSONArray.parseArray(json);
			for(int i = 0; i < array.size(); i++){
				JSONObject obj = (JSONObject)array.get(i);
				dto.setType(obj.getString("type"));
				dto.setCycleType(obj.getString("cycleType"));
				dto.setCycleValue(obj.getString("cycleValue"));
				ProjectParameterDto mDto=service.create(dto);
				if(null==mDto){
					response.fail("80000", "保存数据错误");
					return response;
				}
				response.success(mDto);
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("保存数据错误：{}",e);
		}
		return response;
	}

	@Override
	public void init(ProjectParameterDto saveDto) {
		saveDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		saveDto.setType("0");
		saveDto.setCycleValue("7");
		saveDto.setCycleType("conversionSign");
		service.create(saveDto);
		saveDto.setCycleType("receivable");
		service.create(saveDto);
		saveDto.setCycleValue("30");
		saveDto.setCycleType("giveUp");
		saveDto.setDelFlag("1");
		service.create(saveDto);
	}

	@Override
	public Response<ProjectParameterDto> saveSingleParam(ProjectParameterDto dto, String json) {
		Response<ProjectParameterDto> response=new Response<ProjectParameterDto>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("保存"));
				return response;
			}
			//循环保存新项目参数
			JSONArray array = JSONArray.parseArray(json);
			for(int i = 0; i < array.size(); i++){
				JSONObject obj = (JSONObject)array.get(i);
				//先删除原项目参数
				ProjectParameterDto del=new ProjectParameterDto();
				del.setMerchantId(dto.getMerchantId());
				del.setProjectId(dto.getProjectId());
				dto.setType(obj.getString("type"));
				del.setCycleType(obj.getString("cycleType"));
				del.setDelFlag(DelFlagEnum.UNDEL.getNo());
				service.delByProjectId(del);

				//新增新记录
				dto.setType(obj.getString("type"));
				dto.setCycleType(obj.getString("cycleType"));
				dto.setCycleValue(obj.getString("cycleValue"));
				ProjectParameterDto mDto=service.create(dto);
				if(null==mDto){
					response.fail("80000", "保存数据错误");
					return response;
				}
				response.success(mDto);
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("保存数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Response<Integer> updateByPrimaryKey(ProjectParameterDto dto,Boolean selective) {
		Response<Integer> response=new Response<Integer>();
		try {
			if(null==selective || null==dto || null==dto.getId()){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("更新"));
				return response;
			}
			
			if(selective){
				int count=service.update(dto);
				if(count>0){
					response.success(Integer.valueOf(count));
				}else{
					response.fail("90001", "更新数据失败");
				}
			}else{
				response.fail("90000", "暂未实现,修改参数selective=true即可");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("更新数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Response<Integer> deleteByPrimaryKey(Long id) {
		Response<Integer> response=new Response<Integer>();
		try {
			if(null==id){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("删除"));
				return response;
			}
			ProjectParameterDto mDto=new ProjectParameterDto();
			mDto.setId(id);
			int count=service.destroy(mDto);
			if(count>0){
				response.success(Integer.valueOf(count));
			}else{
				response.fail("90001", "删除数据失败");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("删除数据错误：{}",e);
		}
		return response;
	}
	
    @Override
	public Response<ProjectParameter> findById(Long id) {
		Response<ProjectParameter> response=new Response<ProjectParameter>();
		try {
			if(null==id){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("主键查询"));
				return response;
			}
			ProjectParameter entity=service.selectById(id);
			if(null==entity){
				response.fail("90001", "主键查询数据失败");
			}else{
				response.success(entity);;
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("主键查询数据错误：{}",e);
		}
		return response;
	}

	@Override
	public ResponseList<ProjectParameter> findListByDto(ProjectParameterDto dto) {
		ResponseList<ProjectParameter> response=new ResponseList<ProjectParameter>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("查询"));
				return response;
			}
			List<ProjectParameter> list=service.selectListByDto(dto);
			if(null==list){
				response.fail(ResponseCode.NO_DATA_FOUND.getCode(), ResponseCode.NO_DATA_FOUND.formatMessage("查询"));
				return response;
			}
			response.success(list);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询数据错误：{}",e);
		}
		return response;
	}
	
	@Override
	public Response<ProjectParameter> findByDto(ProjectParameterDto dto) {
		Response<ProjectParameter> response=new Response<ProjectParameter>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("查询"));
				return response;
			}
			ProjectParameter entity = service.selectByDto(dto);
			if(null==entity){
				response.fail(ResponseCode.NO_DATA_FOUND.getCode(), ResponseCode.NO_DATA_FOUND.formatMessage("查询"));
				return response;
			}
			response.success(entity);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Page<ProjectParameterDto> findPageByDto(ProjectParameterDto dto, Integer begin,Integer row) {
		Page<ProjectParameterDto> response=new Page<ProjectParameterDto>();
		try {
			if(null==dto || null==begin || null==row){
				response.setSuccess(false);
				response.setErrorMsg("参数不合法");
				return response;
			}
			dto.setOrders("id");
			Page<ProjectParameterDto> page = service.getPage(dto, begin, row);
			response=page;
			response.setSuccess(true);
		} catch (Exception e) {
			response.setSuccess(false);
			response.setErrorMsg(ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询分页数据错误：{}",e);
		}
		return response;
	}
	
	/**
	 * @param projectId
	 * @return
	 * 查询项目下的业态
	 */
	@Override
	public ResponseList<ProjectParameterDto> getIndustMarkByProjectId(Long projectId) {
		ResponseList<ProjectParameterDto> response=new ResponseList<ProjectParameterDto>();
		try {
			if(null==projectId){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("查询"));
				return response;
			}
			List<ProjectParameterDto> list=service.getIndustMarkByProjectId(projectId);
			if(null==list){
				response.fail(ResponseCode.NO_DATA_FOUND.getCode(), ResponseCode.NO_DATA_FOUND.formatMessage("查询"));
				return response;
			}
			response.success(list);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询数据错误：{}",e);
		}
		return response;
	}

}
